<?php

/**
 * Class for manipulating email
 */
class CmsEmail {

    /**
     * @static send a email
     * @param array $data
     *      array('subject'=>?, 'params'=>array(), 'view'=>?, 'to'=>?, 'from'=>?)
     * @param bool $requireView if true, it will only send email if view is existed
     */
    public static function mail($data, $requireView = false) {
//        self::_setTestEmail($data, 'quocbao1087@gmail.com');
        $message = new YiiMailMessage($data['subject']);
        if (isset($data['view'])) {
            if ($requireView) {
                $path = YiiBase::getPathOfAlias(Yii::app()->mail->viewPath) . '/' . $data['view'] . '.php';
                if (!file_exists($path)) {
                    return;
                }
            }
            $message->view = $data['view'];
        } elseif ($requireView)
            return;
        $message->setBody($data['params']['message'], 'text/html');


        if (is_array($data['to'])) {
            foreach ($data['to'] as $t) {
                $message->addTo($t);
            }
        }
        else
            $message->addTo($data['to']);
        $message->from = $data['from'];
        // Tới chỗ này vẫn đúng nhưng mà cái email nó ra dạng vầy: txhlop12a12@gmail.com => Null
//        var_dump($data['from']);die();
        $message->setFrom(array($data['from'] => $data['from']));
        return Yii::app()->mail->send($message);
    }

    /**
     * @static send separate a mail to each email address
     * @param $data
     */
    public static function mailAll($data) {
        foreach ($data as $d) {
            self::mail($d);
        }
    }

    /**
     * 
     * @static send a mail
     * @template

      $aSubject = array('{KEY_1}' =>'value1',
      '{KEY_2}'=>'value2',
      '{KEY_3}'=>'value3'
      );

      $aBody = array('{KEY_1}' =>'value1',
      '{KEY_2}'=>'value2',
      '{KEY_3}'=>'value3'
      );

     */

    /**
     * 
     * @param int $iEmailTemplateID
     * @param array $aSubject
     * @param array $aBody
     * @param array $sTo
     * @example  
     *          $aSubject = array('{KEY_1}' =>'value1',
      '{KEY_2}'=>'value2',
      '{KEY_3}'=>'value3'
      );

      $aBody = array('{KEY_1}' =>'value1',
      '{KEY_2}'=>'value2',
      '{KEY_3}'=>'value3'
      );
     */
    public static function sendmail($iEmailTemplateID, $aSubject, $aBody, $sTo, $from, $attachment = null) {
        $modelEmailTemplate = EmailTemplates::model()->findByPk($iEmailTemplateID);

        $sSubject = $modelEmailTemplate->email_subject;
        if (is_array($aSubject) && count($aSubject) > 0)
            foreach ($aSubject as $key => $value) {
                $sSubject = str_replace($key, $value, $sSubject);
            }

        $sBody = $modelEmailTemplate->email_body;
        if (is_array($aBody) && count($aBody) > 0)
            foreach ($aBody as $key => $value) {
                $sBody = str_replace($key, $value, $sBody);
            }
        $header = '<div style="background-color: #ebebeb;padding:10px"><table  style="padding:10px;width:600px;margin: 0 auto;background: white;color: black;">'
                . '<tr><td><div class="row-fluid">
                
                    <a style="text-indent: 893px;color: white;" href="http://upbeat.vn">
                        <img class="img-responsive" src="http://upbeat.vn/data/logo/small/logo_13917814872600.png">
                                    <img style="margin-bottom:5px" class="img-responsive" src="http://upbeat.vn/data/logo/slogan_vi_13956573654127.png">
                    </a>
               
                         <p style="border-bottom: 1px solid #ddd;"></p>                                   
                         

                     
            </div></td></tr>';
        $footer = '<tr><td >' . $sBody . '</td></tr></table></div>';
        $sBody = $header . $footer;
        $data = array(
            'subject' => $sSubject,
            'params' => array(
                'message' => $sBody,
            ),
//            'view'=>'message',
            'to' => $sTo,
            'from' => $from,
            'attach' => $attachment
        );
        return CmsEmail::mail($data);
    }

    public static function sendbarcode($sSubject, $sBody, $sTo, $from, $attachment = null) {



        $header = '<div style="background-color: #ebebeb;padding:10px"><table  style="padding:10px;width:600px;margin: 0 auto;background: white;color: black;">'
                . '<tr><td><div class="row-fluid">
                
                    <a style="text-indent: 893px;color: white;" href="http://upbeat.vn">
                        <img class="img-responsive" src="http://upbeat.vn/data/logo/small/logo_13917814872600.png">
                                    <img style="margin-bottom:5px" class="img-responsive" src="http://upbeat.vn/data/logo/slogan_vi_13956573654127.png">
                    </a>
                         <p style="border-bottom: 1px solid #ddd;"></p>                                   
            </div></td></tr>';
        $footer = '<tr><td >' . $sBody . '</td></tr></table></div>';
        $sBody = $header . $footer;
        $data = array(
            'subject' => $sSubject,
            'params' => array(
                'message' => $sBody,
            ),
//            'view'=>'message',
            'to' => $sTo,
            'from' => $from,
            'attach' => $attachment
        );
        return CmsEmail::mail($data);
    }

    /**
     * 
     * @param int $iEmailTemplateID
     * @param array $aSubject
     * @param array $aBody
     * @param string $sTo
     * @copyright (c) 2013, bb 
     */
    public static function sendmailTranslate($iEmailTemplateID, $aSubject, $aBody, $sTo) {
        $modelEmailTemplate = new TranslateHelper('EmailTemplates', $iEmailTemplateID);

        $sSubject = $modelEmailTemplate->get('email_subject');
        if (is_array($aSubject))
            foreach ($aSubject as $key => $value) {
                $sSubject = str_replace($key, $value, $sSubject);
            }

        $sBody = $modelEmailTemplate->get('email_body');
        if (is_array($aBody))
            foreach ($aBody as $key => $value) {
                $sBody = str_replace($key, $value, $sBody);
            }

        $data = array(
            'subject' => $sSubject,
            'params' => array(
                'message' => $sBody,
            ),
            'view' => 'message',
            'to' => $sTo,
            'from' => Yii::app()->params['autoEmail'],
        );
        return CmsEmail::mail($data);
    }

    /**
     * All emails are sent to emailTest
     * @param array $data
     * @param string $emailTest
     * @copyright (c) 7/6/2013, bb 
     */
    private static function _setTestEmail(&$data, $emailTest) {
        if (is_array($data['to'])) {
            foreach ($data['to'] as $key => $email) {
                $data['to'][$key] = $emailTest;
            }
        } else {
            $data['to'] = $emailTest;
        }

        if (isset($data['setCc']))
            $data['setCc'] = $emailTest;

        /*
         * Begin Bcc
         */
        if (isset($data['setBcc'])) {
            if (is_array($data['setBcc'])) {
                foreach ($data['setBcc'] as $key => $email) {
                    $data['setBcc'][$key] = $emailTest;
                }
            } else {
                $data['setBcc'] = $emailTest;
            }
        }
    }

}